/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameSpot;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.Elements;

/**
 *
 * @author Hector
 */
public class CrawlerGameSpot {

    public CrawlerGameSpot() {
    }
    
    private String getURL(int n){
        if(n==1) return "http://www.gamespot.com/reviews.html?mode=all";
        else return "http://www.gamespot.com/reviews.html?mode=all&page="+n;
    }
    
    public ArrayList<ModeloReview> getReviewsList(int paginas) throws IOException{
        ArrayList<ModeloReview> reviews = new ArrayList<ModeloReview>();
        ArrayList<ModeloComment> comments = new ArrayList<ModeloComment>();
        try {
            for (int i = 1; i <= paginas; i++) {//navegar el las páginas, 50 reviews
                Document doc = Jsoup.connect(getURL(1)).userAgent("Mozilla").get();
                Elements revs = doc.getElementById("filter_results").getElementsByClass("body").first().getElementsByTag("table").first().getElementsByTag("tbody").first().getElementsByTag("tr");
                for (Element data : revs) {
                    ModeloReview review = new ModeloReview();
                    String reviewTittle = (data.child(0).text());//titulo
                    String reviewPlatform = (data.child(1).text());//plataforma
                    String reviewScore = (data.child(3).text());//score
                    Document rev = Jsoup.connect(data.getElementsByTag("a").first().attr("href")).userAgent("Mozilla").get();
                    String reviewPubdate = (rev.getElementById("storyHead").getElementsByTag("header").first().getElementsByClass("time").first().getElementsByTag("time").first().attr("datetime"));//fecha
                    String reviewBody = (rev.getElementsByClass("story_body").first().text());//body review
                    String reviewNumberComments = (rev.getElementById("lf_fyre_head").getElementsByClass("lf_head_bar").first().getElementsByClass("lf_comment_count").first().text());//n° comentarios
                    String reviewPeopleListening = (rev.getElementById("lf_fyre_head").getElementsByClass("lf_head_bar").first().getElementsByClass("lf_listener_count").first().text());//personas escuchando
                    String reviewMetacriticScore = (rev.select("li.review_score.criticScore").first().getElementsByClass("wrap").first().getElementsByClass("data").first().text());//metacritic score
                    String reviewUserScore = (rev.select("li.review_score.community_score").first().getElementsByClass("wrap").first().getElementsByClass("data").text());//user score
                    String reviewAuthor = (rev.getElementById("storyHead").getElementsByTag("header").first().getElementsByClass("authorByline").first().text());//autor
                    for (Iterator<Element> it = rev.getElementById("lf_comments").getElementsByClass("lf_comment_container").iterator(); it.hasNext();) {
                        Element element = it.next();
                        String commentBody = (element.getElementsByClass("lf_comment_body").first().getElementsByClass("lf_body").first().text());
                        String commentRatingUser = (element.getElementsByClass("lf_comment_body").first().getElementsByClass("lf_comment_user_rating").first().text());
                        String commentUser = (element.getElementsByClass("lf_comment_body").first().getElementsByClass("lf_comment_user_nick").first().text());
                        comments.add(new ModeloComment(commentUser, commentBody, commentRatingUser));
                    }
                    reviews.add(new ModeloReview(reviewTittle, reviewAuthor, reviewPubdate, reviewPlatform, reviewBody, (ArrayList<ModeloComment>)comments.clone(), reviewScore,"","","", reviewNumberComments, reviewPeopleListening, reviewMetacriticScore, reviewUserScore));
                    comments.clear();
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.toString());
        }
        return reviews;
    }
}
